{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "architectural-privacy",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "informal-feelings",
   "metadata": {},
   "outputs": [],
   "source": [
    "res_oct = pd.read_csv('./res/oct.csv')\n",
    "res_boct = pd.read_csv('./res/boct.csv')\n",
    "res_mfoct = pd.read_csv('./res/mfoct.csv')\n",
    "res_sk = pd.read_csv('./res/sk.csv')\n",
    "res = {'OCT':res_oct, 'binOCT':res_boct, 'flowOCT':res_mfoct, 'CART':res_sk}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "smooth-watch",
   "metadata": {},
   "outputs": [],
   "source": [
    "def resProcess(res):\n",
    "    \"\"\"\n",
    "    Calculate the average results from random data split\n",
    "    Select alpha with highest validation accuracy\n",
    "    \"\"\"\n",
    "    if 'alpha' in res.columns:\n",
    "        res_group = res.groupby(['instance', 'depth', 'alpha']).mean().reset_index()\n",
    "        res_group = res_group.groupby(['instance', 'depth']) \\\n",
    "                             .apply(lambda group: group[group['val_acc'] == group['val_acc'].max()]) \\\n",
    "                             .drop_duplicates(subset=['instance', 'depth']) \\\n",
    "                             .reset_index(drop=True)\n",
    "    else:\n",
    "        res_group = res.groupby(['instance', 'depth']).mean().reset_index()\n",
    "    return res_group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "federal-jersey",
   "metadata": {},
   "outputs": [],
   "source": [
    "def resCombine(res, feat):\n",
    "    df = pd.DataFrame(columns=['instance', 'depth'])\n",
    "    for r in res:\n",
    "        df_cur = resProcess(res[r])[['instance', 'depth', feat]]\n",
    "        df_cur = df_cur.rename(columns={feat:r})\n",
    "        df = df.merge(df_cur, how='outer', on=['instance', 'depth'])\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "crazy-poison",
   "metadata": {},
   "outputs": [],
   "source": [
    "resCombine(res, 'test_acc').to_csv('./res/res.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "accepted-washer",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
